package com.winchain.chainbill.basic.invoice.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.basic.invoice.dao.BasInvoiceDao;
import com.winchain.chainbill.basic.invoice.dao.BasInvoiceRelDao;
import com.winchain.chainbill.basic.invoice.po.BasInvoice;
import com.winchain.chainbill.basic.invoice.po.BasInvoiceRel;
import com.winchain.chainbill.basic.invoice.rx.BasInvoicePassMsgSubcriber;
import com.winchain.chainbill.basic.invoice.service.BasInvoiceService;
import com.winchain.chainbill.basic.invoice.vo.BasInvoiceEvent;
import com.winchain.chainbill.basic.invoice.vo.BasInvoiceVo;

import rx.Observable;
import tk.mybatis.mapper.entity.Example;

@Service("basInvoiceService")
public class BasInvoiceServiceImpl extends PersistentServiceImpl<BasInvoice> implements BasInvoiceService {
	@Autowired
	private BasInvoiceRelDao basInvoiceRelDao;
	@Autowired
	private BasInvoiceDao basInvoiceDao;
	@Autowired
	private BasInvoicePassMsgSubcriber basInvoicePassMsgSubcriber;

	@Pagination
	public List<BasInvoice> selectBasInvoicesByPage(PageLimit limit, BasInvoiceVo basInvoiceVo) {
		Example example = new Example(BasInvoice.class);
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtil.emptyAndNull(basInvoiceVo.getInvoiceState())) {
			criteria.andEqualTo("invoiceState", basInvoiceVo.getInvoiceState());
		}
		if (!StringUtil.emptyAndNull(basInvoiceVo.getCusName())) {
			criteria.andLike("cusName", StringUtil.getParameterLike(basInvoiceVo.getCusName()));
		}
		if (!StringUtil.emptyAndNull(basInvoiceVo.getApplyStartDate())) {
			criteria.andGreaterThanOrEqualTo("applyDate", basInvoiceVo.getApplyStartDate());
		}
		if (!StringUtil.emptyAndNull(basInvoiceVo.getApplyEndDate())) {
			criteria.andLessThanOrEqualTo("applyDate", basInvoiceVo.getApplyEndDate());
		}
		if (basInvoiceVo.getInvoiceMinAmt() != null) {
			criteria.andGreaterThanOrEqualTo("invoiceAmt", basInvoiceVo.getInvoiceMinAmt());
		}
		if (basInvoiceVo.getInvoiceMaxAmt() != null) {
			criteria.andLessThanOrEqualTo("invoiceAmt", basInvoiceVo.getInvoiceMaxAmt());
		}
		if (!StringUtil.emptyAndNull(basInvoiceVo.getCusId())) {
			criteria.andEqualTo("cusId", basInvoiceVo.getCusId());
		}
		if (!StringUtil.emptyAndNull(basInvoiceVo.getInvoiceType())) {
			criteria.andEqualTo("invoiceType", basInvoiceVo.getInvoiceType());
		}
		return selectByExample(example);
	}

	@Pagination
	public List<BasInvoice> selectBasInvoicesForMgtByPage(PageLimit limit, BasInvoiceVo basInvoiceVo) {
		if (!StringUtil.emptyAndNull(basInvoiceVo.getCusName())) {
			basInvoiceVo.setCusName(StringUtil.getParameterLike(basInvoiceVo.getCusName()));
		}
		return this.basInvoiceDao.selectBasInvoicesForMgtByPage(basInvoiceVo);
	}

	public boolean saveInvoice(BasInvoice basInvoice, String relJson) throws Exception {
		basInvoice.setInvoiceId(SystemUtil.getUUID());
		basInvoice.setCusId(CommonDataUtil.getCurEntId());
		basInvoice.setInvoiceState("0");
		if (!StringUtil.emptyAndNull(relJson)) {
			List<BasInvoiceRel> rels = JsonUtil.fromJsonArray(relJson, BasInvoiceRel.class);
			for (BasInvoiceRel rel : rels) {
				rel.setInvoiceId(basInvoice.getInvoiceId());
				rel.setInvoiceType(basInvoice.getInvoiceType());
				this.basInvoiceRelDao.insert(rel);
			}
		}
		return save(basInvoice) == 1;
	}

	public String pass(String invoiceIds) throws Exception {
		if (!StringUtil.emptyAndNull(invoiceIds)) {
			String[] ids = invoiceIds.split(",");
			int count = 0;
			int hasDoCount = 0;
			for (String invoiceId : ids) {
				BasInvoice basInvoice = (BasInvoice) selectByKey(invoiceId);
				if (!basInvoice.getInvoiceState().equals("0")) {
					hasDoCount++;
				} else {
					basInvoice.setInvoiceState("1");
					count += updateNotNull(basInvoice);

					BasInvoiceEvent event = new BasInvoiceEvent();
					event.setValue("cusId", basInvoice.getCusId());
					event.setValue("applyDate", basInvoice.getApplyDate());
					event.setValue("invoiceAmt", basInvoice.getInvoiceAmt());
					Observable<BasInvoiceEvent> observers = Observable.just(event);
					observers.subscribe(this.basInvoicePassMsgSubcriber);
				}
			}
			if ((hasDoCount == 0) && (count == ids.length)) {
				return "success";
			}
			if ((hasDoCount == 1) && (ids.length == 1)) {
				return "done";
			}
			if ((hasDoCount > 0) && (ids.length > count)) {
				return "pdone";
			}
			return "fail";
		}
		return "success";
	}
}
